<?php

    class CEan128
    {
	var $aCode;
	
	var $iStartCode;
	var $iStopCode;
	
	var $iBarWidth;
    
	static function &GetInstance()
	{
	    global $g_hObjects;
	    static $m_hApp = NULL;
					
	    if(!isset($m_hApp))
	    {
	        $m_hApp = new CEan128();
	        $g_hObjects[] = $m_hApp;
	        $m_hApp->OnInit();
	    }
											    
	    return $m_hApp;
	}
	
	function OnInit()
	{
	    //ean start code
	    $this->iStartCode = '211214';
	    $this->iStopCode  = '2331112';
	
	    //single bar width
	    $this->iBarWidth = 1;
	
	    //initializing the bar and space pattern  for the Code128 B character set
	    
	     $this->aCode[0] = "212222";  // " "
	     $this->aCode[1] = "222122";  // "!"
	     $this->aCode[2] = "222221";  // "{QUOTE}"
	     $this->aCode[3] = "121223";  // "#"
	     $this->aCode[4] = "121322";  // "$"
	     $this->aCode[5] = "131222";  // "%"
	     $this->aCode[6] = "122213";  // "&"
	     $this->aCode[7] = "122312";  // "'"
	     $this->aCode[8] = "132212";  // "("
	     $this->aCode[9] = "221213";  // ")"
	     $this->aCode[10] = "221312"; // "*"
	     $this->aCode[11] = "231212"; // "+"
	     $this->aCode[12] = "112232"; // ","
	     $this->aCode[13] = "122132"; // "-"
	     $this->aCode[14] = "122231"; // "."
             $this->aCode[15] = "113222"; // "/" 
             $this->aCode[16] = "123122"; // "0"	       
             $this->aCode[17] = "123221"; // "1"		       
             $this->aCode[18] = "223211"; // "2"			       
    	     $this->aCode[19] = "221132"; // "3"
	     $this->aCode[20] = "221231"; // "4"
	     $this->aCode[21] = "213212"; // "5"
	     $this->aCode[22] = "223112"; // "6"
	     $this->aCode[23] = "312131"; // "7"
	     $this->aCode[24] = "311222"; // "8"
	     $this->aCode[25] = "321122"; // "9"
	     $this->aCode[26] = "321221"; // ":"
	     $this->aCode[27] = "312212"; // ";"
	     $this->aCode[28] = "322112"; // "<"
	     $this->aCode[29] = "322211"; // "="
	     $this->aCode[30] = "212123"; // ">"
	     $this->aCode[31] = "212321"; // "?"
	     $this->aCode[32] = "232121"; // "@"
	     $this->aCode[33] = "111323"; // "A"
	     $this->aCode[34] = "131123"; // "B"
	     $this->aCode[35] = "131321"; // "C"
	     $this->aCode[36] = "112313"; // "D"
	     $this->aCode[37] = "132113"; // "E"
	     $this->aCode[38] = "132311"; // "F"
	     $this->aCode[39] = "211313"; // "G"
	     $this->aCode[40] = "231113"; // "H"
	     $this->aCode[41] = "231311"; // "I"
	     $this->aCode[42] = "112133"; // "J"
	     $this->aCode[43] = "112331"; // "K"
	     $this->aCode[44] = "132131"; // "L"
	     $this->aCode[45] = "113123"; // "M"
	     $this->aCode[46] = "113321"; // "N"
	     $this->aCode[47] = "133121"; // "O"
	     $this->aCode[48] = "313121"; // "P"
	     $this->aCode[49] = "211331"; // "Q"
	     $this->aCode[50] = "231131"; // "R"
    	     $this->aCode[51] = "213113"; // "S"
             $this->aCode[52] = "213311"; // "T"
	     $this->aCode[53] = "213131"; // "U"
             $this->aCode[54] = "311123"; // "V"
	     $this->aCode[55] = "311321"; // "W"
	     $this->aCode[56] = "331121"; // "X"
	     $this->aCode[57] = "312113"; // "Y"
	     $this->aCode[58] = "312311"; // "Z"
	     $this->aCode[59] = "332111"; // "["
	     $this->aCode[60] = "314111"; // "\"
	     $this->aCode[61] = "221411"; // "]"
	     $this->aCode[62] = "431111"; // "^"
	     $this->aCode[63] = "111224"; // "_"
	     $this->aCode[64] = "111422"; // "`"
	     $this->aCode[65] = "121124"; // "a"
	     $this->aCode[66] = "121421"; // "b"
	     $this->aCode[67] = "141122"; // "c"
	     $this->aCode[68] = "141221"; // "d"
	     $this->aCode[69] = "112214"; // "e"
	     $this->aCode[70] = "112412"; // "f"
	     $this->aCode[71] = "122114"; // "g"
	     $this->aCode[72] = "122411"; // "h"
	     $this->aCode[73] = "142112"; // "i"
	     $this->aCode[74] = "142211"; // "j"
	     $this->aCode[75] = "241211"; // "k"
	     $this->aCode[76] = "221114"; // "l"
	     $this->aCode[77] = "413111"; // "m"
	     $this->aCode[78] = "241112"; // "n"
	     $this->aCode[79] = "134111"; // "o"
	     $this->aCode[80] = "111242"; // "p"
	     $this->aCode[81] = "121142"; // "q"
	     $this->aCode[82] = "121241"; // "r"
	     $this->aCode[83] = "114212"; // "s"
	     $this->aCode[84] = "124112"; // "t"
	     $this->aCode[85] = "124211"; // "u"
	     $this->aCode[86] = "411212"; // "v"
	     $this->aCode[87] = "421112"; // "w"
	     $this->aCode[88] = "421211"; // "x"
	     $this->aCode[89] = "212141"; // "y"
	     $this->aCode[90] = "214121"; // "z"
	     $this->aCode[91] = "412121"; // "{"
	     $this->aCode[92] = "111143"; // "|"
	     $this->aCode[93] = "111341"; // "}"
	     $this->aCode[94] = "131141"; // "~"
	     $this->aCode[95] = "114113"; // 95
	     $this->aCode[96] = "114311"; // 96
	     $this->aCode[97] = "411113"; // 97
	     $this->aCode[98] = "411311"; // 98
	     $this->aCode[99] = "113141"; // 99
	     $this->aCode[100] = "114131"; // 100
	     $this->aCode[101] = "311141"; // 101
	     $this->aCode[102] = "411131"; // 102									    						      
	}
	
	function OnDestroy()
	{
	}
	
	//return the Ean128 code for a character
	function GetCharCode($cChr)
	{
	    return $this->aCode[ord($cChr) - 32];
	}
	
	//get the Code128 numerical equivalent of a character
	function GetCharNumber($cChr)
	{
	    return ord($cChr) - 32;
	}
	
	function GetNumCode($iIdx)
	{
	    return $this->aCode[$iIdx];
	}
	
	function Draw($szTxt)
	{
	    $iCkSum    = 104;
	    $szAllBars = $this->iStartCode;
	    
	    for($i = 0; $i < strlen($szTxt); $i++)
	    {
		$cChr = $szTxt[$i];
		
		$iVal = $this->GetCharNumber($cChr);
		
		$iCkSum += ($iVal * ($i + 1));
		
		$szAllBars .= $this->GetCharCode($cChr);
	    }
	    
	    $szAllBars .= $this->GetNumCode($iCkSum % 103);
	    $szAllBars .= $this->iStopCode;
	    
	    //10 pixels at the front & end for quiet zone
	    $iBarCodeWidth = 20;
	    
	    //bar code height 
	    $iBarCodeHeight = 80;
	    
	    for($i = 0; $i < strlen($szAllBars); $i++)
	    {
		$iBarCodeWidth += $szAllBars[$i] * $this->iBarWidth;
	    }
	    
	    $hImg = imagecreate($iBarCodeWidth,$iBarCodeHeight+15);
	    
	    $hBlack = imagecolorallocate($hImg, 0, 0, 0);
	    $hWhite = imagecolorallocate($hImg, 255, 255, 255);
	    
	    imagefill($hImg,0,0,$hWhite);
	    
	    $iPosX = 10;
	    
	    $iBar = 1;
	    
	    for($i = 0; $i < strlen($szAllBars); $i++)
	    {
		$iWidth = $szAllBars[$i] * $this->iBarWidth;
		
		if($iBar == 1)
		{
		    imagefilledrectangle($hImg, $iPosX, 0, $iPosX + $iWidth - 1, $iBarCodeHeight, $hBlack);
		    
		    $iPosX += $iWidth;
		    
		    $iBar = 0;
		}
		else
		{
		    $iPosX += $iWidth;
		    
		    $iBar = 1;
		}
	    }
	    
	    for($i = 0; $i < strlen($szTxt); $i++)
	    {
		imagechar($hImg,6,5+13*$i,$iBarCodeHeight+2,$szTxt[$i],$hBlack);
	    }
	    
	    HttpContentType(CONTENT_GIF);
	    
            imagegif($hImg);
            imagedestroy($hImg);	    
	}
								
    }

?>